package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.BankCardVO;
import com.sc.nft.controller.vo.SandWithdrawRecordVO;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.UserAffirmDTO;
import com.sc.nft.entity.dto.sand.SmsDTO;
import com.sc.nft.entity.vo.BalanceVO;
import com.sc.nft.entity.vo.HfFastBankVO;
import com.sc.nft.entity.vo.PaymentMethodVO;
import com.sc.nft.entity.vo.SandTransferRecordVO;
import com.sc.nft.enums.AccountStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.helper.HfPayHelper;
import com.sc.nft.helper.SandAccountHelper;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.PayOrderService;
import com.sc.nft.service.SandTransferRecordService;
import com.sc.nft.service.UserInfoService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriUtils;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("pay/hfPay")
@Api(tags = "汇付接口")
public class HfPayController {
    private final PayOrderService payOrderService;
    private final HfPayHelper hfPayHelper;
    private final HfFastBankDao hfFastBankDao;
    private final UserHfExtDao userHfExtDao;


    @ApiOperation(value = "汇付开户", notes = "汇付开户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hfOpenAccount", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<String> hfOpenAccount(@RequestBody  H5Request request) {
        if (true) {
            throw new GlobalRunTimeException("该钱包已下线");
        }
        UserInfo user = UserPool.getUser();
        return Result.success(payOrderService.HfOpenAccount(user,request.getIsWeb()));
    }



    @ApiOperation(value = "汇付钱包管理", notes = "汇付钱包管理", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hfWallet", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<String> hfWallet(@RequestBody H5Request request) {
        UserInfo user = UserPool.getUser();
        return Result.success(payOrderService.HfWallet(user,request.getIsWeb()));
    }

//    @ApiOperation(value = "分账", notes = "分账", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PostMapping(value = "/sub", produces = MediaType.APPLICATION_JSON_VALUE)
//    public Result<String> hfWallet(@RequestBody SubRequest request) {
//        return Result.success(hfPayHelper.subLedger(request.getOrgOrderId(),request.getOrgOrderDate(),request.getAmount()).toJSONString());
//    }

    @ApiOperation(value = "汇付绑定银行卡", notes = "汇付绑定银行卡", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hfBindCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HfFastBankVO> hfBindCard(@Valid @RequestBody BindBankCardRequest request) {
        UserInfo user = UserPool.getUser();
        return Result.success(hfPayHelper.bindBankCard(user.getId(),request.getId(),request.getCardNo(),request.getPhoneNo()));
    }

    @ApiOperation(value = "重发绑卡短信", notes = "重发绑卡短信", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hfBindCardSendSms", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<String> hfBindCardSendSms(@Valid @RequestBody IdRequest request) {
        UserInfo user = UserPool.getUser();
        HfFastBank hfFastBank = hfFastBankDao.getById(request.getId());
        if(ObjectUtil.isNull(hfFastBank)||hfFastBank.getUserId().longValue()!=user.getId().longValue()){
            return Result.fail(ErrorCode.NOT_FOUND_BANK_CARD);
        }
        if(BooleanUtil.isTrue(hfFastBank.getStatus())){
            return Result.fail("该银行卡已绑定成功");
        }
        JSONObject result  = hfPayHelper.bindCardSendSms(user.getId(),hfFastBank.getIdentityNo(),hfFastBank.getName(),hfFastBank.getBankCardNo(),hfFastBank.getMobile());
        return Result.success(result.getString("order_id"));
    }
    @ApiOperation(value = "绑卡确认", notes = "绑卡确认", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hfBindCardConfirm", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result hfBindCardConfirm(@Valid @RequestBody BindHfCardSmsConfirmRequest request) {
        hfPayHelper.bindBankCardConfirm(UserPool.getUser().getId(),request.getOrderId(),request.getSmsCode(),request
                .getId());
        return Result.success();
    }


    @ApiOperation(value = "银行卡列表", notes = "银行卡列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/cardList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<HfFastBankVO>> cardList() {
        List<HfFastBank> listByUserId = hfFastBankDao.getListByUserId(UserPool.getUser().getId());
        for (HfFastBank hfFastBank : listByUserId) {
            hfFastBank.setBankCardNo("****"+hfFastBank.getBankCardNo().substring(hfFastBank.getBankCardNo().length()-4));
        }
        return Result.successColl(listByUserId,HfFastBankVO.class);
    }


    @ApiOperation(value = "解绑银行卡", notes = "解绑银行卡", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/remove", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result remove(@Valid @RequestBody IdRequest request) {
        HfFastBank hfFastBank = hfFastBankDao.getById(request.getId());
        if(ObjectUtil.isNull(hfFastBank)||UserPool.getUser().getId().longValue()!=hfFastBank.getUserId().longValue()){
            throw new GlobalRunTimeException("未找到银行卡");
        }
        hfFastBank.deleteById();
        return Result.success();
    }
}


